<?php
namespace Api\Controller;
use Think\Controller;
class ArticlecommentController extends Controller{
    public function index()
    {
        echo 'article';

    }


    // 发表日志
    public function add_article()
    {
        if (!$_SESSION['islogin'] == 2) {
            $this->error('请登录后再操作', 'http://localhost/Qzone/index.php/Home/Users/Login');
        } else {

            //判断传输数据
            if (isset($_POST['Article_Title']) && isset($_POST['Article_Content']) &&
                !empty($_POST['Article_Title']) && !empty($_POST['Article_Content'])
            ) {

                $data['Article_Title'] = I('post.Article_Title');
                $data['Article_Content'] = I('post.Article_Content');
                $data['User_Id'] = $_SESSION['user_id'];
                $data['User_Name'] = $_SESSION['user_name'];
                $ar = M('article');
                if ($ar->add($data)) {
                    echo BaseController::ApiSuccess('发表成功');
                    exit;
                } else {
                    echo BaseController::ApiError('发表失败');
                    exit;
                }
            }
        }
    }

    //获取日志
    public function get_article(){
        if (!$_SESSION['islogin'] == 2) {
            $this->error('请登录后再操作', 'http://localhost/Qzone/index.php/Home/Users/Login');
        } else {
            if(IS_POST){
                $data['User_Id'] = $_SESSION['user_id'];
                $art = M('article');
                $result = $art -> where($data)->select();
                if($result){
                    echo BaseController::ApiSuccess($result);exit;
                }
            }
        }
    }

    //发表评论
    public function add_t(){
        if (!$_SESSION['islogin'] == 2) {
            $this->error('请登录后再操作', 'http://localhost/Qzone/index.php/Home/Users/Login');
        } else {

            if (isset($_POST['article_id']) && isset($_POST['content'])) {
                $Con = D('Articlecomment');
                if ($Con->create()) {
                    $data['uid'] = $_SESSION['user_id'];
                    $data['article_id'] = I('post.article_id');
                    $data['content'] = I('post.content');
                    $Con->add($data);
                    echo BaseController::ApiSuccess('发表成功！');
                    exit;
                } else {
                    echo BaseController::ApiError($Con->getError());
                    exit;
                }
            }
        }
    }


    //获取评论
        public function get_t(){
            if(isset($_POST['article_id'])){
                $data['article_id'] = I('post.article_id');
                $Con = M('Articlecomment');
                $rl = $Con -> where($data) -> select();
                //var_dump($result);
            if ($rl){
                    echo BaseController::ApiSuccess($rl);
                }else{
                    echo BaseController::ApiError('暂时没有评论');  exit;
                }

            }
        }
}